//
//  ACell.m
//  Study1
//
//  Created by ChenJie on 2/10/20.
//  Copyright © 2020 OrgName. All rights reserved.
//

#import "ACell.h"
#import <SDWebImage/SDWebImage.h>
#import "NSString+size.h"
#import "UIView+location.h"
#import "UIView+name.h"

#define kMarginH 15
#define kMarginV 15
#define kSpaceBetweenV 5
#define kSpaceBetweenH 5
#define kIconDimension 30

#define kTitleFont [UIFont systemFontOfSize:20]
#define kContentFont [UIFont systemFontOfSize:14]

#define kScreenWidth [UIScreen mainScreen].bounds.size.width

typedef NS_ENUM(NSUInteger, ComponentId) {
    ComponentId_iconImageView = 1,
    ComponentId_titleLabel,
    ComponentId_contentLabel,
};

//

@interface ACell ()

@property (nonatomic, strong) UIImage *iconImage;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *content;
@property (nonatomic, copy) NSString *iconUrlString;

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation ACell

#pragma mark - Lifecycle methods

//+ (void)load {
/// Do not keep an empty +load method
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        //
        [self.contentView addSubview:self.iconImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contentLabel];
        
        //
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.iconImageView.frame = [self.viewModel.layouts[@(ComponentId_iconImageView)] CGRectValue];
    self.titleLabel.frame = [self.viewModel.layouts[@(ComponentId_titleLabel)] CGRectValue];
    self.contentLabel.frame = [self.viewModel.layouts[@(ComponentId_contentLabel)] CGRectValue];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
}

#pragma mark - Public methods

+ (CGFloat)cellHeightWithCellModel:(ACellVM *)cellModel {
    
    return [self cellHeightWithIconImage:cellModel.iconImage
                                   title:cellModel.title
                                 content:cellModel.content
                                 layouts:cellModel.layouts];
}

+ (CGFloat)cellHeightWithIconImage:(UIImage *)iconImage
                             title:(NSString *)title
                           content:(NSString *)content
                           layouts:(NSMutableDictionary *)layouts{
    
    CGFloat height;
    
    CGFloat topMark = 0;
    CGFloat leftMark = 0;
    CGFloat rightMark = 0;
    
    //
    leftMark += kMarginH;
    topMark += kMarginV;
    CGRect iconFrame = CGRectMake(leftMark, topMark, kIconDimension, kIconDimension);
//    self.iconImageView.frame = CGRectMake(leftMark, topMark, kIconDimension, kIconDimension);
    leftMark += kIconDimension;
    
    //
    leftMark += kSpaceBetweenH;
    rightMark += kMarginH;
    CGFloat labelWidthMax = kScreenWidth - leftMark - rightMark;
    

    
//    CGSize titleLabelSize = [NSString sizeWithText:title maxWidth:labelWidthMax font:kTitleFont];
    CGSize titleLabelSize = [title sizeWithMaxWidth:labelWidthMax font:kTitleFont];
    
    CGRect titleFrame = CGRectMake(leftMark, topMark, titleLabelSize.width, titleLabelSize.height);
//    self.titleLabel.frame = CGRectMake(leftMark, topMark, titleLabelSize.width, titleLabelSize.height);
    topMark += titleLabelSize.height;
    
    //
    topMark += kSpaceBetweenV;
    CGSize contentLabelSize = [content sizeWithMaxWidth:labelWidthMax font:kContentFont];
    CGRect contentFrame = CGRectMake(leftMark, topMark, contentLabelSize.width, contentLabelSize.height);
    topMark += contentLabelSize.height;
    
    //
    height = topMark + kMarginV;
    
    layouts[@(ComponentId_iconImageView)] = [NSValue valueWithCGRect:iconFrame];
    layouts[@(ComponentId_titleLabel)] = [NSValue valueWithCGRect:titleFrame];
    layouts[@(ComponentId_contentLabel)] = [NSValue valueWithCGRect:contentFrame];
    
    return height;
    
}

#pragma mark - Private methods



#pragma mark - Event response methods

- (void)iconImageViewDidClick:(UITapGestureRecognizer *)tap {
    if ([self.delegate respondsToSelector:@selector(cell:iconDidClick:)]) {
        [self.delegate cell:self iconDidClick:tap.view];
    }
}

#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(ACellVM *)viewModel {
    _viewModel = viewModel;
    
    //
    self.iconImage = viewModel.iconImage;
    self.title = viewModel.title;
    self.content = viewModel.content;
    
    
    //
    self.iconUrlString = viewModel.iconUrlString;
    self.iconImageView.name = viewModel.title;
}

- (void)setIconImage:(UIImage *)iconImage {
    _iconImage = iconImage;
    
    //
    self.iconImageView.image = iconImage;
}

- (void)setIconUrlString:(NSString *)iconUrlString {
    _iconUrlString = iconUrlString;
    
    //
    NSURL *iconUrl = [NSURL URLWithString:iconUrlString];
    [self.iconImageView sd_setImageWithURL:iconUrl
                                 completed:
     ^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//        NSLog(@"iconUrlString: %@, cache type: %ld", iconUrlString, (long)cacheType);
        if (image && cacheType == SDImageCacheTypeNone) {
            self.iconImageView.alpha = 0;
            [UIView animateWithDuration:0.65 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                self.iconImageView.alpha = 1;
            } completion:^(BOOL finished) {

            }];
        }
    }];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    
    //
    self.titleLabel.text = title;
}

- (void)setContent:(NSString *)content {
    _content = content;
    
    //
    self.contentLabel.text = content;
}

//- (void)setLayouts:(NSDictionary *)layouts {
//    _layouts = layouts;
//
//    NSLog(@"current layout is: %@", layouts);
//}

#pragma mark - Getter

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = [UIColor lightGrayColor];
        imageView.layer.cornerRadius = kIconDimension / 2.f;
        imageView.clipsToBounds = YES;
        imageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap
        = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                  action:@selector(iconImageViewDidClick:)];
        [imageView addGestureRecognizer:tap];
        
        
        _iconImageView = imageView;
    }
    return _iconImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        UILabel *label = [UILabel new];
//        label.backgroundColor = [UIColor lightGrayColor];
        label.font = kTitleFont;
        label.numberOfLines = 0;
        
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        UILabel *label = [UILabel new];
//        label.backgroundColor = [UIColor lightGrayColor];
        label.font = kContentFont;
        label.numberOfLines = 0;
        
        _contentLabel = label;
    }
    return _contentLabel;
}

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
